/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.activity.CenterListActivity;
import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.activity.ListStoreActivity;
import com.epfresh.api.utils.*;
import com.epfresh.R;
import com.epfresh.bean.HomeCommTag;
import com.epfresh.bean.HomeTop;
import com.epfresh.bean.HomeTopBean;
import com.epfresh.bean.MarketBean;
import com.epfresh.views.tag.TagCloudView;
import com.umeng.analytics.MobclickAgent;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import com.epfresh.utils.*;

public class ViewBinderHomeGoodsOne
    extends ItemViewBinder<HomeTopBean.GoodsOne, ViewBinderHomeGoodsOne.ViewHolder> implements View.OnClickListener{
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    int imageWidth;
    int tagImageWidth;
    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if(o instanceof HomeTopBean.GoodsOne) {
            HomeTopBean.FreshestBean bean = ((HomeTopBean.GoodsOne)o).goods;
            MobclickAgent.onEvent(v.getContext(), "Click_Click_HomeRecommend");/*统计广告点击*/
            Intent commIntent = new Intent(v.getContext(),
                    DetailGoodsActivity.class);
            Log.e("id____",bean.getId());
            commIntent.putExtra("storeId", bean.getStoreId());
            commIntent.putExtra("id", bean.getId());
            commIntent.putExtra("image_url", bean.getThumbnail());
            commIntent.putExtra("promotionItemId", bean.getPromotionItemId());
            commIntent.putExtra("title", bean.getTitle());
            v.getContext().startActivity(commIntent);
        }
    }

    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.view_home_goods_one, parent, false);
        imageWidth = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(52))/3;
        tagImageWidth = imageWidth/3;
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeTopBean.GoodsOne bean) {
        HomeTopBean.FreshestBean goods = bean.goods;
        if(goods==null)
            return;
        initPicAndTag(holder,goods);
        if(goods.isTop()) {
            holder.iv_top.setVisibility(View.VISIBLE);
        }else{
            holder.iv_top.setVisibility(View.GONE);
        }
        holder.tv_title.setText(bean.goods.getTitle());
        if("2".equals(bean.goods.getType())){
            Drawable drawable= holder.tv_title.getResources().getDrawable(R.mipmap.pre_text);
            // 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.tv_title.setCompoundDrawables(drawable,null,null,null);
        }else{
            holder.tv_title.setCompoundDrawables(null,null,null,null);
        }
        holder.tv_package.setText(bean.goods.getPack());
        holder.tv_price.setText(bean.goods.getPrice()+"元/");
        holder.tv_price_.setText(bean.goods.getChargeUnit());
        String oldPrice = bean.goods.getOriginalPrice();
        if(oldPrice!=null&&!"".equals(oldPrice)) {
            holder.tv_old_price.setText(bean.goods.getOriginalPrice() + "元/" + bean.goods.getChargeUnit());
            holder.tv_old_price.setVisibility(View.VISIBLE);
            holder.tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        }
        else{
            holder.tv_old_price.setVisibility(View.GONE);
        }

        if("1".equals(bean.goods.getPromotionItemType())){//1促销；2秒杀
            holder.tv_activity_type.setVisibility(View.VISIBLE);
        }else {
            holder.tv_activity_type.setVisibility(View.GONE);
        }
        holder.tv_store_info.setText(bean.goods.getMarketName()+"  "+bean.goods.getStoreName());
        holder.tv_store_time.setText(TimeStringUtils.initTime(bean.goods.getModifiedTime(),null));
        holder.iv_product.setTag(R.id.item_key_status, bean);
        holder.iv_product.setOnClickListener(this);
        holder.itemView.setTag(R.id.item_key_status, bean);
        holder.itemView.setOnClickListener(this);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_product,iv_tag,iv_top;//iv_type;title + drawable? 预售？
        TextView tv_title,tv_package,tv_price,tv_price_,tv_old_price,tv_store_info,tv_store_time,tv_activity_type;
        TagCloudView tag;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_activity_type = (TextView) itemView.findViewById(R.id.tv_activity_type);
            iv_product = (ImageView) itemView.findViewById(R.id.iv_product);
            iv_tag = (ImageView) itemView.findViewById(R.id.iv_tag);
            iv_top = (ImageView) itemView.findViewById(R.id.iv_top);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_package = (TextView) itemView.findViewById(R.id.tv_package);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_price_ = (TextView) itemView.findViewById(R.id.tv_price_);
            tv_old_price = (TextView) itemView.findViewById(R.id.tv_old_price);
            tag = (TagCloudView) itemView.findViewById(R.id.ll_tags);
            tv_store_info = (TextView) itemView.findViewById(R.id.tv_store_info);
            tv_store_time = (TextView) itemView.findViewById(R.id.tv_store_time);
        }
    }

    void initPicAndTag(@NonNull ViewHolder holder, @NonNull HomeTopBean.FreshestBean goods){
        ViewGroup.LayoutParams params =  holder.iv_product.getLayoutParams();
        params.width = imageWidth;
        params.height = (int)(imageWidth*0.75);
        holder.iv_product.setLayoutParams(params);
        ViewGroup.LayoutParams tagParams =  holder.iv_tag.getLayoutParams();
        tagParams.width = tagImageWidth;
        tagParams.height = tagImageWidth;
        holder.iv_tag.setLayoutParams(tagParams);
        if(goods.getBanners()==null||goods.getBanners().size()<1) {
            ImageLoader.getInstance().displayImage("", holder.iv_product);
        }
        else{
            ImageLoader.getInstance().displayImage(goods.getBanners().get(0), holder.iv_product);
        }
        if (goods.getPromotionMarkUrl() != null && !"".equals(goods.getPromotionMarkUrl()) && !"null".equals(goods.getPromotionMarkUrl())){
            ImageLoader.getInstance().displayImage(goods.getPromotionMarkUrl(), holder.iv_tag);
            holder.iv_tag.setVisibility(View.VISIBLE);
        }
        else
            holder.iv_tag.setVisibility(View.INVISIBLE);

        ArrayList<HomeCommTag> tags = goods.getTags();
        if (tags != null && tags.size() > 0) {
            holder.tag.setVisibility(View.VISIBLE);
        } else {
            holder.tag.setVisibility(View.GONE);
        }
        holder.tag.setCommTags(tags);
    }
}
